package pl.nazaweb.imedproplus.business.service;

import java.math.BigInteger;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import pl.nazaweb.imedproplus.model.entity.OrganizationStructure;
import pl.nazaweb.imedproplus.model.facade.OrganizationStructureFacade;

/**
 *
 * @author naza
 */
@Stateless
@LocalBean
public class OrganizationStructureService {

    @EJB
    private OrganizationStructureFacade organizationStructureFacade;

    public List<OrganizationStructure> getChildsByParent(BigInteger parentId) {
        return this.organizationStructureFacade.getOrganizationStructureParentChilds(parentId);
    }

    public boolean allowAddOrganizationStructureToTree(Long id) {
        return this.organizationStructureFacade.isOrganizationStrucutreInTree(id) ? false : true;
    }

    public List<OrganizationStructure> getOrganizationStructureList() {
        return this.organizationStructureFacade.findAll();
    }

    public void addNewOrganizationStructure(OrganizationStructure organizationStructure) {
        organizationStructure.setParent(null);
        organizationStructure.setId(null);
        this.organizationStructureFacade.create(organizationStructure);
    }

    public void addNewOrgaanizationStructures(List<OrganizationStructure> structures) {
        for (OrganizationStructure structure : structures) {
            this.addNewOrganizationStructure(structure);
        }
    }

    public void updateOrganizationStructure(OrganizationStructure organizationStructure) {
        this.organizationStructureFacade.edit(organizationStructure);
    }

    public void updateOrganizationStructures(List<OrganizationStructure> structures) {
        for (OrganizationStructure structure : structures) {
            this.updateOrganizationStructure(structure);
        }
    }

    public void removeOrganizationStructure(OrganizationStructure organizationStructure) {
        this.organizationStructureFacade.remove(organizationStructure);
    }

    public void removeOrgaanizationStructures(List<OrganizationStructure> structures) {
        for (OrganizationStructure structure : structures) {
            this.removeOrganizationStructure(structure);
        }
    }
    
    public void addOrganizationStructureToStructureTree(BigInteger parentId, Long childId){
        this.organizationStructureFacade.addOrganizationStructureToStructureTree(parentId, childId);
    }
    
    public void removeOrganizationStructureFromTree(Long id, BigInteger parentId){
        this.organizationStructureFacade.removeOrganizationStructureFromTree(id, parentId);
    }
}
